//
//  ViewDisplayViewController.m
//  FuncFanc
//
//  Created by hubing on 2018/3/16.
//  Copyright © 2018年 huhu. All rights reserved.
//

#import "ViewDisplayViewController.h"
#import <AVKit/AVKit.h>

@interface ViewDisplayViewController ()<AVPlayerViewControllerDelegate>
@property (nonatomic, strong) AVPlayerViewController *moviePlayer;
@property (nonatomic) CGRect defaultFrame;

@end

@implementation ViewDisplayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.moviePlayer = [[AVPlayerViewController alloc]init];
    self.moviePlayer.delegate = self;
    
    AVPlayer *avplayer = [[AVPlayer alloc]init];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:avplayer];
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    playerLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:playerLayer];
    
        
//    ALMoviePlayerControls *movieControls = [[ALMoviePlayerControls alloc] initWithMoviePlayer:self.moviePlayer style:ALMoviePlayerControlsStyleDefault];
//    [movieControls setBarColor:[UIColor colorWithRed:195/255.0 green:29/255.0 blue:29/255.0 alpha:0.5]];
//    [movieControls setTimeRemainingDecrements:YES];
//    [movieControls setFadeDelay:2.0];
//    [movieControls setBarHeight:100.f];
//    [movieControls setSeekRate:2.f];
//
//    [self.moviePlayer setControls:movieControls];
//
//    [self.view addSubview:self.moviePlayer.view];
//    [self.moviePlayer setContentURL:[NSURL URLWithString:_url]];

}


- (void)configureViewForOrientation:(UIInterfaceOrientation)orientation {
    CGFloat videoWidth = 0;
    CGFloat videoHeight = 0;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        videoWidth = 700.f;
        videoHeight = 535.f;
    } else {
        videoWidth = self.view.frame.size.width;
        videoHeight = 220.f;
    }
    
    //calulate the frame on every rotation, so when we're returning from fullscreen mode we'll know where to position the movie plauyer
    self.defaultFrame = CGRectMake(self.view.frame.size.width/2 - videoWidth/2, self.view.frame.size.height/2 - videoHeight/2, videoWidth, videoHeight);
    
    //only manage the movie player frame when it's not in fullscreen. when in fullscreen, the frame is automatically managed
//    if (self.moviePlayer.isFullscreen)
//        return;
//
//    //you MUST use [ALMoviePlayerController setFrame:] to adjust frame, NOT [ALMoviePlayerController.view setFrame:]
//    [self.moviePlayer setFrame:self.defaultFrame];
}
- (void)localFile {
//    [self.moviePlayer stop];
//    [self.moviePlayer setContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"popeye" ofType:@"mp4"]]];
//    [self.moviePlayer play];
}

- (void)remoteFile {
//    [self.moviePlayer stop];
//    [self.moviePlayer setContentURL:[NSURL URLWithString:@"http://archive.org/download/WaltDisneyCartoons-MickeyMouseMinnieMouseDonaldDuckGoofyAndPluto/WaltDisneyCartoons-MickeyMouseMinnieMouseDonaldDuckGoofyAndPluto-HawaiianHoliday1937-Video.mp4"]];
//    [self.moviePlayer play];
}
- (void)moviePlayerWillMoveFromWindow {
    //movie player must be readded to this view upon exiting fullscreen mode.
    if (![self.view.subviews containsObject:self.moviePlayer.view])
        [self.view addSubview:self.moviePlayer.view];
    
    //you MUST use [ALMoviePlayerController setFrame:] to adjust frame, NOT [ALMoviePlayerController.view setFrame:]
//    [self.moviePlayer setFrame:self.defaultFrame];
}

- (BOOL)shouldAutorotate {
    return YES;
}
     


@end
